<?php
/**
* +---------------------------------------------------------------------------+
* | $Id:$ 																	  |
* +---------------------------------------------------------------------------+
* | $Rev:$ 																  |
* +---------------------------------------------------------------------------+
* | Copyright (c) 2002-2005 Linuxpowered, Inc |
* +---------------------------------------------------------------------------+
* | Author: Vidyut Luther |
* +---------------------------------------------------------------------------+
* $URL: http://svn.linuxpowered.com/binder/admin/add_domains.php $
**/ 

$section = "ADMIN";
$title = "Binder: Add Groups"; 
 
require_once '../global.inc.php'; 
$userlevel = $_SESSION['userlevel']; 
$curuser = $_SESSION['USERID']; 

$tpl->assign("foruser",$curuser); 


$mode = $_POST['action']; 
if($mode == 'grpadd') { 
  $groupname = $_POST['groupname']; 
  $default_ip = $_POST['default_ip']; 
  if(empty($default_ip)) {
    echo "<strong> Default IP Cannot be left blank, hit the back button and try again </strong> "; 
    die;
  }
  if(groupAdd($groupname,$curuser,$default_ip)) {
    header("Location: $web_base/admin/add_groups.php"); 
  }
}
if($userlevel  == "0") {
  $grouplist = groupList($curuser=NULL,$searchval);
} else {
  $grouplist = groupList($curuser,$searchval); 
}


$tpl->assign("groups",$grouplist); 

$tpl->display('addgroups.html'); 





/**
 * groupAdd: Adds a group for a specific user
 * @param $groupname: 
 * @param $foruser: 
 * 
 * @return 
 * @access 
 **/
function groupAdd($groupname,$foruser,$default_ip)
{
  global $db ; 
  require_once INCLUDE_PATH . '/Records.class.php'; 
  $objR =& new DNSRecord(); 

  $defaultip = $objR->_ipToint($default_ip); 

  $sql = "INSERT INTO server_groups (group_name,group_owner,default_ip) VALUES ('$groupname','$foruser','$defaultip') "; 

  if($db->mInsert($sql)) {
    return true; 
  } else {
    echo "Ooops there was a problem with <strong> $sql </strong> Dieing.....<Br> "; 
  }
}


/**
 * groupList: Returns an array of groups for a specific user, or all users if the user is an admin
 * @param $curuser: 
 * @param $searchval: 
 * 
 * @return 
 * @access 
 **/
function groupList($curuser,$searchval)
{
  global $db;
  
  if(!is_null($curuser)) {
    $sql = "SELECT group_id,group_name,INET_NTOA(default_ip) AS default_ip FROM server_groups WHERE group_owner='$curuser' ORDER BY group_name";
  } else {
    $sql =  "SELECT group_id,group_name,INET_NTOA(default_ip) AS default_ip FROM server_groups ORDER BY group_name";
  }

  
  $rs = $db->mRun($sql); 
  while($row=mysql_fetch_assoc($rs)) {
    $grouplist[] = $row ; 
  }

  return $grouplist; 
}
?>
